package com.inspur.s57.datacatalog.service.impl;

import com.inspur.s57.datacatalog.dao.DataAttributeDAO;
import com.inspur.s57.datacatalog.entity.DataAttribute;
import com.inspur.s57.datacatalog.service.DataAttributeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;


@Slf4j
@Service
public class DataAttributeServiceImpl implements DataAttributeService {

    @Autowired
    private DataAttributeDAO dataAttributeDAO;

    @Override
    @Transactional(rollbackOn = Exception.class)
    public boolean saveAttribute(DataAttribute attribute) {
        try {
            dataAttributeDAO.save(attribute);
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    public Page<DataAttribute> findDataAttributesByClassIdAndNameLike(Integer classId, String name, Pageable pageable) {
        return dataAttributeDAO.findDataAttributesByClassIdAndNameLike(classId, "%"+name+"%", pageable);
    }

    @Override
    public DataAttribute findDataAttributeById(Integer id) {
        return dataAttributeDAO.findById(id).get();
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public boolean deleteAttributeById(Integer id) {
        try {
            dataAttributeDAO.deleteById(id);
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    public List<DataAttribute> findDataAttributesByClassId(Integer classId) {
        return dataAttributeDAO.findDataAttributesByClassIdEquals(classId);
    }
}
